import '../models/class_info_model.dart';
import '../manger/network_utils.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';

// 封装返回结果
class LoadClassInfoResult {
  final bool success;
  final List<ClassListInfoModel>? classListModel;
  LoadClassInfoResult({required this.success, this.classListModel});
}

// 单例管理器
class ClassInfoManager {
  static final ClassInfoManager _instance = ClassInfoManager._internal();
  factory ClassInfoManager() => _instance;
  ClassInfoManager._internal();

  // 全局数据
  List<ClassListInfoModel>? _classListModel;

  // 获取全局数据方法
  List<ClassListInfoModel>? get classInfoModel => _classListModel;

  // 加载分类页信息
  Future<LoadClassInfoResult> loadInfoData() async {
    try {
      final response = await NetworkUtils.post(
        isSimplifiedChinese() ? RequestCmd.classList : RequestCmd.classListen,
        body: null,
      );

      final model = ClassListModel.fromJson(response);

      if (model.code == 200) {
        _classListModel = model.data;
        return LoadClassInfoResult(success: true, classListModel: model.data);
      } else {
        return LoadClassInfoResult(success: false);
      }
    } catch (e) {
      return LoadClassInfoResult(success: false);
    }
  }
}
